﻿using System;
using System.Windows.Forms;
using ApplicationGenerator.GeneratorMenu.BusinessLogic;

namespace ApplicationGenerator.GeneratorMenu.Forms
{
    /// <summary>
    /// Kontrolka do wyboru elementu menu
    /// </summary>
    public partial class ApplicationMenuControl : UserControl
    {
        public ApplicationMenuControl()
        {
            InitializeComponent();
            ApplicationMenu applicationMenu = new ApplicationMenu();
            TreeNode rootNode = new TreeNode();
            rootNode.Text = "Menu";
            rootNode.Tag = applicationMenu;

            ctlMenuTree.Nodes.Add(rootNode);
        }

        public ApplicationMenuControl(ApplicationMenu applicationMenu)
        {
            InitializeComponent();
            this.SuspendLayout();
            TreeNode rootNode = new TreeNode();
            rootNode.Text = "Menu";
            rootNode.Tag = applicationMenu;
            ctlMenuTree.Nodes.Add(rootNode);
            InitializeTree(applicationMenu, rootNode);
            this.ResumeLayout();
            this.Refresh();
            Application.DoEvents();
        }

        private void InitializeTree(ApplicationMenuBase baseItem, TreeNode baseNode)
        {
            foreach (var baseMenuElement in baseItem.Items)
            {
                TreeNode newNode = new TreeNode();
                newNode.Name = newNode.Text = baseMenuElement.Text;
                newNode.Tag = baseMenuElement;
                baseNode.Nodes.Add(newNode);

                InitializeTree(baseMenuElement, newNode);
            }
        }

        private void ctlAdd_Click(object sender, EventArgs e)
        {
            if (ctlMenuTree.SelectedNode != null)
            {
                ApplicationMenuItem newItem = new ApplicationMenuItem();
                newItem.Text = "NowyElement";

                TreeNode newNode = new TreeNode();
                newNode.Name = newNode.Text = newItem.Text;
                newNode.Tag = newItem;
                ctlMenuTree.SelectedNode.Nodes.Add(newNode);
                (ctlMenuTree.SelectedNode.Tag as ApplicationMenuBase).Items.Add(newItem);
            }
        }

        private void ctlPropertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (e.ChangedItem.PropertyDescriptor.Name == "Text")
            {
                if (ctlMenuTree.SelectedNode != null)
                {
                    ctlMenuTree.SelectedNode.Text = e.ChangedItem.Value as string;
                }
            }
        }

        public ApplicationMenu GetMenu()
        {
            return ctlMenuTree.Nodes[0].Tag as ApplicationMenu;
        }

        public bool Validate()
        {
            if ((ctlMenuTree.Nodes[0].Tag as ApplicationMenu).Items.Count == 0)
            {
                MessageBox.Show("Należy podać co najmniej jeden element menu");
                return false;
            }

            foreach (var elementMenu in (ctlMenuTree.Nodes[0].Tag as ApplicationMenu).Items)
            {
                if (string.IsNullOrEmpty(elementMenu.Text)
                    || string.IsNullOrEmpty(elementMenu.Url))
                {
                    MessageBox.Show("Każdy element musi mieć nazwę oraz url");
                    return false;
                }
            }
            return true;
        }

        private void ctlMenuTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            ctlPropertyGrid.SelectedObject = e.Node.Tag;
        }

        private void ctlRemove_Click(object sender, EventArgs e)
        {
            if (ctlMenuTree.SelectedNode != null && ctlMenuTree.Nodes[0] != ctlMenuTree.SelectedNode)
            {
                (ctlMenuTree.SelectedNode.Parent.Tag as ApplicationMenuBase).Items.Remove((ctlMenuTree.SelectedNode.Tag as ApplicationMenuItem));
                ctlMenuTree.SelectedNode.Remove();
            }
        }
    }
}
